package main

import (
	"fmt"
	"sort"
	"strconv"
	"strings"
)

var idRange = "1-5"

func newVlanId(ids string, idUsed []int) int {
	fmt.Println("----------new-id-")
	vlanRange := strings.Split(idRange, "-")
	vlanMin, _ := strconv.Atoi(vlanRange[0])
	vlanMax, _ := strconv.Atoi(vlanRange[1])

	if len(idUsed) == 0 {
		return vlanMin
	} else if len(idUsed) > 0 &&  len(idUsed) < vlanMax - vlanMin + 1 {
		//vlan id升序遍历，选择空闲的id
		sort.Ints(idUsed)
		newId := vlanMin
		for _, v := range idUsed {
			if v > newId {
				break
			}
			newId = v + 1
		}
		return newId
	}

	return -1
}

func main() {
	nums := []int{3}
	sort.Ints(nums)
	fmt.Println(nums)


	id1 := newVlanId(idRange, nums)
	fmt.Println("id1=", id1)
	nums = append(nums, id1)
	fmt.Println(nums)

	id2 := newVlanId(idRange, nums)
	nums = append(nums, id2)
	fmt.Println("id2=", id2)
	fmt.Println(nums)

	id3 := newVlanId(idRange, nums)
	nums = append(nums, id3)
	fmt.Println("id3=", id3)
	fmt.Println(nums)

	id4 := newVlanId(idRange, nums)
	nums = append(nums, id4)
	fmt.Println("id4=", id4)
	fmt.Println(nums)

	id5 := newVlanId(idRange, nums)
	nums = append(nums, id5)
	fmt.Println("id5=", id5)
	fmt.Println(nums)

	id6 := newVlanId(idRange, nums)
	nums = append(nums, id6)
	fmt.Println("id5=", id6)
	fmt.Println(nums)


}
